<template>
    <div class="dynamicDetails">
        <navigationBar :bgType="3" :backType="2" navigationTitle="动态详情" />
        <swiper
            v-if="infoData.images && infoData.images.length > 0"
            indicator-dots
            autoplay
            circular
            interval="2000"
            duration="1000"
        >
            <swiper-item v-for="(item, index) in infoData.images" :key="index">
                <image :src="item" mode="scaleToFill" />
            </swiper-item>
        </swiper>

        <video class="video" v-if="infoData.file" :src="infoData.file" />

        <view class="card mb-20 info" v-if="infoData.technician">
            <view class="techInfo">
                <view class="flex align-center just-between top">
                    <view class="left flex align-center">
                        <image
                            :src="infoData.technician.work_image"
                            mode="scaleToFill"
                        />
                        <view class="name">
                            <text>{{ infoData.technician.name }}</text>
                            <view
                                class="tag online"
                                v-if="infoData.technician.is_service == 1"
                            >
                                可服务
                            </view>
                            <view class="tag rest" v-else> 休息中 </view>
                        </view>
                    </view>

                    <view class="btn" @click.stop="showModal" v-if="!isMine">
                        预约ta
                    </view>
                </view>

                <view class="title" v-if="infoData.name">
                    {{ infoData.name }}
                </view>

                <view class="desc">
                    {{ infoData.content }}
                </view>

                <view
                    class="bottom flex just-between align-center"
                    v-if="!isMine"
                >
                    <view class="time">
                        发布于：{{ infoData.createtime_text }}
                    </view>
                    <view class="location">
                        <image
                            class="location_img"
                            src="/static/static/technician/order/location.png"
                            mode="scaleToFill"
                        />
                        <text>{{ infoData.distance }}km</text>
                    </view>
                </view>
            </view>
        </view>

        <!-- 评论 -->
        <view
            class="comment mb-20 card"
            v-if="
                commentList && infoData.technician && status != 0 && status != 2
            "
        >
            <view class="title f-32">全部评论</view>
            <view
                class="item line"
                v-for="(item, index) in commentList"
                :key="index"
            >
                <view class="info flex align-center just-between" v-if="item">
                    <view class="left flex align-center">
                        <view class="avatar">
                            <image
                                class="img"
                                :src="$imageURL + item.user.avatar"
                                mode="scaleToFill"
                            />
                        </view>
                        <view class="name">
                            {{ item.user.nickname }}
                        </view>
                    </view>
                    <view class="time">
                        {{ item.createtime_text }}
                    </view>
                </view>
                <view class="content" v-if="item">
                    {{ item.content }}
                </view>
            </view>
            <emptyData v-if="commentList.length === 0" :text="'暂无评论'" />
        </view>

        <view
            class="comment card"
            v-if="isMine == 1 && status != 1 && infoData.technician"
        >
            <view class="loading flex column align-center" v-if="status == 0">
                <image
                    class="img"
                    src="/static/static/technician/joinUs/toBeReviewedLoading.png"
                    mode="scaleToFill"
                />
                <view class="m-20">等待审核</view>
                <view class="m-20">你的动态正在审核中</view>
            </view>
            <view
                class="errReviewed flex column align-center"
                v-if="status == 2"
            >
                <image
                    class="img"
                    src="/static/static/technician/joinUs/errReviewed.png"
                    mode="scaleToFill"
                />
                <view class="m-20">审核被驳回</view>
                <view class="m-20">
                    驳回原因：代发多福多寿发斯蒂芬地方的 大沙发斯蒂芬但是的
                </view>
            </view>
        </view>

        <view
            class="btns flex just-between align-center"
            v-if="status === null"
        >
            <view class="inputBox flex align-center">
                <image
                    class="icon"
                    src="/static/static/technician/joinUs/edit.png"
                    mode="scaleToFill"
                />
                <input
                    class="input"
                    v-model="commentText"
                    placeholder="说点什么吧~"
                />
            </view>

            <view class="like_box flex align-center just-between" @click="like">
                <image
                    v-if="infoData.is_like == 1"
                    class="like"
                    src="/static/static/technician/choose/like_select.png"
                    mode="scaleToFill"
                />
                <image
                    v-else
                    class="like"
                    src="/static/static/technician/choose/like_normal.png"
                    mode="scaleToFill"
                />
                {{ infoData.like_num || 0 }}
            </view>

            <view class="btn" @click="submit"> 发送 </view>
        </view>

        <view class="btns flex just-around align-center" v-else>
            <view class="btn_del" @click="deleteDynamic">删除动态</view>
            <view class="btn_edit" @click="editDynamic">
                {{ status == 0 ? "编辑动态" : "重新编辑" }}
            </view>
        </view>

        <view class="mask" @click="visible = false" v-if="visible"></view>
        <view class="card service" v-if="visible && infoData.technician">
            <view class="tech">
                <view class="info">
                    <image
                        class="img"
                        :src="infoData.technician.work_image"
                        mode="scaleToFill"
                    />
                    <view class="name">{{ infoData.technician.name }}</view>
                    <text>从业{{ infoData.technician.practise_year }}年</text>
                </view>
                <view class="more" @click="goToDetail">
                    {{ "查看详情>>" }}
                </view>
            </view>

            <view class="list">
                <block v-for="(item, index) in serviceList" :key="index">
                    <view class="item flex align-center">
                        <view class="file">
                            <image :src="item.image" />
                        </view>

                        <view class="right flex column just-between">
                            <view class="one flex align-base just-between">
                                <view class="title fb-30 limit-num-line">
                                    {{ item.name }}
                                </view>

                                <view class="num f-24">
                                    {{ item.total_sales }}人已体验
                                </view>
                            </view>

                            <view class="sign-bg flex" scroll-x>
                                <block
                                    v-for="(sign, idx) in item.subtitle.split(
                                        '|'
                                    )"
                                    :key="idx"
                                >
                                    <view class="sign f-26">{{ sign }}</view>
                                </block>
                            </view>

                            <view class="bottom flex align-end">
                                <view class="time f-20">
                                    {{ item.duration_time }}分钟
                                </view>

                                <view class="price flex align-base">
                                    <text class="f-26">¥</text>
                                    <text class="f-40">{{ item.price }}</text>
                                </view>

                                <view class="old-price f-26">
                                    ￥{{ item.original_price }}
                                </view>
                            </view>

                            <view class="btn f-26 flex align-center">
                                <view
                                    class="compute"
                                    @click="onNumChangeSub(item, index)"
                                    v-if="item.number > 0"
                                >
                                    <image
                                        class="sub"
                                        src="/static/static/technician/joinUs/sub.png"
                                        mode="scaleToFill"
                                    />
                                </view>
                                <text v-if="item.number > 0">{{
                                    item.number
                                }}</text>
                                <view
                                    class="compute add"
                                    @click="onNumChangeAdd(item, index)"
                                >
                                    <image
                                        class="add"
                                        src="/static/static/technician/joinUs/add.png"
                                        mode="scaleToFill"
                                    />
                                </view>
                            </view>
                        </view>
                    </view>
                </block>
            </view>
        </view>

        <view v-if="visible" class="btnss flex just-between align-center">
            <view class="allprice">
                合计：<text class="f-32 price">
                    ¥ {{ total_price.toFixed(2) }}
                </text>
            </view>
            <view class="btn" @click="submitOrder"> 提交订单 </view>
        </view>
    </div>
</template>

<script>
import {
    getDynamicDetail,
    getCommentsList,
    publishComment,
    likeCancel,
    deleteDynamic,
} from "@/API/API/recruit.js";
export default {
    data() {
        return {
            id: "",
            lat: "",
            lng: "",
            infoData: {},
            commentList: [],
            serviceList: [],
            page: 1,
            total: 0,
            commentText: "",
            ready: false,
            visible: false,
            num: 0,
            total_price: 0,
            chooseIndex: null,
            isMine: 0,
            status: null,
        };
    },

    onLoad(options) {
        this.id = options.dynamic_id;
        this.lat = options.lat || 0;
        this.lng = options.lng || 0;
        this.isMine = options.isMine || 0;
        this.status = options.status || null;
        this.getData();
    },

    methods: {
        editDynamic() {
            if (this.infoData.images && this.infoData.images.length > 0) {
                uni.navigateTo({
                    url: `/subPages/technician/recruit/addDynamic?id=${
                        this.id
                    }&type=photo&infoData=${JSON.stringify(this.infoData)}`,
                });
            } else {
                uni.navigateTo({
                    url: `/subPages/technician/recruit/addDynamic?id=${
                        this.id
                    }&type=video&infoData=${JSON.stringify(this.infoData)}`,
                });
            }
        },
        deleteDynamic() {
            deleteDynamic({ dynamic_id: this.id }).then((res) => {
                uni.showToast({ title: res.msg, icon: "none" });
                setTimeout(() => {
                    uni.navigateBack();
                }, 800);
            });
        },
        like() {
            likeCancel({
                dynamic_id: this.id,
            });
            if (this.infoData.is_like == 0) {
                this.infoData.is_like = 1;
                this.infoData.like_num++;
            } else {
                this.infoData.is_like = 0;
                this.infoData.like_num--;
            }
        },
        init(index) {
            if (this.chooseIndex === null) {
                this.chooseIndex = index;
            }
            if (index !== this.chooseIndex) {
                this.num = 0;
            }

            this.serviceList.forEach((item) => {
                if (item.number > 0) {
                    this.num++;
                }
            });
            if (this.num == 1 && index !== this.chooseIndex) {
                uni.showToast({
                    title: "最多只能选择一个项目",
                    icon: "none",
                });
                return false;
            } else {
                this.chooseIndex = index;
                return true;
            }
        },
        onNumChangeAdd(item, index) {
            if (this.init(index)) {
                this.total_price = 0;
                item.number++;
                this.$forceUpdate();
                this.serviceList.forEach((item) => {
                    this.total_price += item.price * item.number;
                });
            }
        },
        onNumChangeSub(item) {
            if (item.number > 0) {
                item.number--;
                this.$forceUpdate();

                this.total_price = 0;
                this.serviceList.forEach((item) => {
                    this.total_price += item.price * item.number;
                });
            }
        },
        showModal() {
            if (this.infoData.technician.is_service == 1) {
                this.visible = true;
            } else {
                uni.showToast({
                    title: "该技师休息中",
                    icon: "none",
                });
            }
        },
        getData() {
            getDynamicDetail({
                dynamic_id: this.id,
                lat: this.lat,
                lng: this.lng,
            })
                .then((res) => {
                    console.log(res);
                    this.infoData = res.data;
                    res.data.service.data.forEach((item) => {
                        item.number = 0;
                    });
                    this.serviceList.push(...res.data.service.data);
                    this.serviceTotal = res.data.service.total;
                    this.getComment();
                })
                .catch((err) => {
                    console.log(err);
                    uni.showToast({
                        title: err.msg || "服务器繁忙",
                        icon: "none",
                    });
                });
        },
        getComment() {
            getCommentsList({
                dynamic_id: this.id,
                page: this.page,
            })
                .then((res) => {
                    this.commentList = res.data.data;
                    this.total = res.data.total;
                    this.ready = true;
                })
                .catch((err) => {
                    uni.showToast({
                        title: err.msg || "服务器繁忙",
                        icon: "none",
                    });
                });
        },

        submit() {
            if (!this.commentText) {
                uni.showToast({
                    title: "请输入评论内容",
                    icon: "none",
                });
                return;
            }
            publishComment({
                dynamic_id: this.id,
                content: this.commentText,
            })
                .then((res) => {
                    uni.showToast({
                        title: res.msg,
                        icon: "none",
                    });
                    this.commentText = "";
                    this.getComment();
                })
                .catch((err) => {
                    uni.showToast({
                        title: err.msg || "服务器繁忙",
                        icon: "none",
                    });
                });
        },

        submitOrder() {
            if (this.total_price == 0) {
                uni.showToast({
                    title: "请选择服务项目",
                    icon: "none",
                });
                return;
            }
            uni.navigateTo({
                url: `/subPages/technician/home/confirmOrder?serviceID=${
                    this.serviceList[this.chooseIndex].id
                }&technicianID=${this.infoData.technician.id}&num=${
                    this.serviceList[this.chooseIndex].number
                }`,
            });
        },

        goToDetail() {
            uni.navigateTo({
                url: `/subPages/technician/home/technicianDetail?technicianID=${
                    this.infoData.technician.id
                }&serviceID=${this.serviceList[this.chooseIndex || 0].id}&lat=${
                    this.lat
                }&lng=${this.lng}`,
            });
        },
    },
};
</script>
<style lang="scss" scoped>
.dynamicDetails {
    padding-bottom: 130rpx;
}
swiper,
video {
    width: 750rpx;
    height: 750rpx;
    swiper-item {
        width: 750rpx;
        height: 750rpx;
        image,
        video {
            width: 750rpx;
            height: 750rpx;
        }
    }
}
/deep/.uni-swiper-dot-active {
    background: $theme-color !important;
}
.info {
    .top {
        margin-bottom: 20rpx;
    }
    .techInfo {
        // display: flex;
        // align-items: center;
        // justify-content: space-between;
        image {
            width: 80rpx;
            height: 80rpx;
            border-radius: 50%;
            margin-right: 20rpx;
        }
        .name {
            font-size: 30rpx;
            color: $title-color;
            text {
                line-height: 1.5rem;
            }
            .tag {
                font-size: 24rpx;
                width: 108rpx;
                height: 36rpx;
                line-height: 36rpx;
                text-align: center;
                color: white;
                padding: 4rpx 10rpx;
                border-radius: 20rpx;
            }
            .online {
                background: linear-gradient(90deg, #4f5052, #101010);
            }
            .rest {
                background: #b0b0b0;
            }
        }
        .btn {
            width: 140rpx;
            height: 60rpx;
            font-size: 28rpx;
            color: white;
            background: $theme-color;
            border-radius: 10rpx;
            text-align: center;
            line-height: 60rpx;
        }

        .title {
            font-size: 28rpx;
            line-height: 1.5;
        }

        .desc {
            font-size: 26rpx;
            color: #666666;
            line-height: 1.5;
        }

        .bottom {
            font-size: 24rpx;
            margin-top: 20rpx;

            .time {
                margin-right: 20rpx;
                font-size: 24rpx;
                color: #999999;
            }

            .location {
                display: flex;
                align-items: center;

                .location_img {
                    width: 20rpx;
                    height: 22rpx;
                    margin-right: 10rpx;
                }
            }
        }
    }
}

.comment {
    .title {
        font-size: 32rpx;
        color: #333333;
        margin-bottom: 20rpx;
    }
    .item {
        padding: 20rpx 0 30rpx 0;
        border-top: 2rpx solid #eaeaea;
        border-bottom: 2rpx solid #eaeaea;
        .info {
            .avatar {
                width: 52rpx;
                height: 52rpx;
                border-radius: 50%;
                margin-right: 20rpx;

                .img {
                    width: 52rpx;
                    height: 52rpx;
                    border-radius: 50%;
                }
            }

            .name {
                font-size: 24rpx;
                color: #999999;
            }
        }

        .content {
            font-size: 28rpx;
            line-height: 1.5;
        }
    }
    .item:last-child {
        border-bottom: none;
    }
}
.card {
    background: white;
    padding: 20rpx;
}

.mb-20 {
    margin-bottom: 20rpx;
}

.btnss {
    background: white;
    width: 100vw;
    height: 130rpx;
    box-sizing: border-box;
    position: fixed;
    bottom: 0;
    padding: 0 24rpx;
    box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.1);
    z-index: 10;

    .allprice {
        font-size: 30rpx;
        color: $theme-color;
        .price {
            font-size: 36rpx;
            font-weight: bold;
            color: $price-color;
        }
    }
    .btn {
        width: 166rpx;
        height: 60rpx;
        line-height: 60rpx;
        background: $grad-theme-color;
        border-radius: 40rpx;
        color: white;
        text-align: center;
        font-size: 30rpx;
    }
}

.btns {
    background: white;
    width: 100vw;
    height: 130rpx;
    box-sizing: border-box;
    position: fixed;
    bottom: 0;
    padding: 0 24rpx;
    box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.1);
    z-index: 9;

    .inputBox {
        height: 60rpx;
        line-height: 60rpx;
        background: #f5f6f8;
        border-radius: 40rpx;
        padding: 0 20rpx;
        width: 70%;

        .input {
            width: 100%;
        }

        .icon {
            width: 24rpx;
            height: 24rpx;
            margin-right: 20rpx;
        }
    }
    .btn {
        width: 136rpx;
        height: 60rpx;
        line-height: 60rpx;
        background: $grad-theme-color;
        border-radius: 40rpx;
        color: white;
        text-align: center;
        font-size: 30rpx;
    }
}

.mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    z-index: 8;
}
.service {
    background: white;
    padding: 20rpx;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 9;
    border-top-left-radius: 20rpx;
    border-top-right-radius: 20rpx;
    .tech {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 20rpx;
        border-bottom: 2rpx solid #ececec;
        .info {
            display: flex;
            align-items: center;

            .img {
                width: 84rpx;
                height: 84rpx;
                border-radius: 50%;
                margin-right: 10rpx;
            }
            text {
                font-size: 24rpx;
                color: #999999;
                margin-left: 10rpx;
            }
        }
        .more {
            color: $theme-color;
            font-size: 24rpx;
        }
    }

    .list {
        max-height: 730rpx;
        padding: 20rpx 0 150rpx 0;
        overflow-y: auto;

        .item {
            width: 100%;
            background: white;
            margin-bottom: 20rpx;
            border-radius: 20rpx;
            overflow: hidden;
            padding: 30rpx 0rpx;
            box-sizing: border-box;

            &:last-child {
                margin-bottom: 0rpx;
            }

            .file {
                width: 176rpx;
                height: 176rpx;
                border-radius: 20rpx;
                overflow: hidden;

                > image {
                    width: 100%;
                    height: 100%;
                }
            }

            .right {
                margin-left: 20rpx;
                flex: 1;
                overflow: hidden;
                min-height: 144rpx;
                position: relative;

                .one {
                    width: 100%;
                    overflow: hidden;

                    .title {
                        --limit-num: 1;
                        margin-right: 10rpx;
                        flex: 1;
                        overflow: hidden;
                    }
                    .num {
                        color: $old-price-color;
                    }
                }

                .sign-bg {
                    width: 100%;
                    overflow: hidden;
                    white-space: nowrap;

                    .sign {
                        display: inline-block;
                        margin-right: 20rpx;
                        color: $sub-title-color;

                        &:last-child {
                            margin-right: 0rpx;
                        }
                    }
                }

                .bottom {
                    width: 100%;
                    overflow: hidden;

                    .time {
                        height: 28rpx;
                        line-height: 28rpx;
                        text-align: center;
                        border-radius: 4rpx;
                        padding: 0rpx 6rpx;
                        background: $time-grad-color;
                        color: $time-title-color;
                    }

                    .price {
                        margin-left: 10rpx;
                        color: $price-color;
                    }

                    .old-price {
                        margin-left: 12rpx;
                        color: $old-price-color;
                        text-decoration: line-through;
                    }
                }

                .btn {
                    position: absolute;
                    right: 0;
                    bottom: 0;
                    width: 140rpx;
                    height: 52rpx;
                    line-height: 52rpx;
                    color: #000;
                    justify-content: end;
                    .compute {
                        width: 38rpx;
                        height: 38rpx;
                        border: 2rpx solid $theme-color;
                        border-radius: 10rpx;
                        position: relative;
                        .sub {
                            width: 16rpx;
                            height: 2rpx;
                            position: absolute;
                            left: 50%;
                            top: 50%;
                            transform: translate(-50%, -50%);
                        }

                        .add {
                            width: 16rpx;
                            height: 16rpx;
                            position: absolute;
                            left: 50%;
                            top: 50%;
                            transform: translate(-50%, -50%);
                        }
                    }
                    .add {
                        background: #606dce;
                    }
                    text {
                        margin: 0 20rpx;
                    }
                }
            }
        }
    }
}
.like_box {
    padding: 0 20rpx;
    box-sizing: border-box;
    .like {
        width: 40rpx;
        height: 40rpx;
        margin-right: 10rpx;
    }
}
.m-20 {
    margin: 20rpx 0;
}
.btn_del {
    width: 330rpx;
    height: 94rpx;
    line-height: 94rpx;
    text-align: center;
    border: 2rpx solid $theme-color;
    border-radius: 50rpx;
    color: $theme-color;
}
.btn_edit {
    width: 330rpx;
    height: 94rpx;
    line-height: 94rpx;
    text-align: center;
    background: $grad-theme-color;
    border-radius: 50rpx;
    color: white;
}
</style>